const HandleMeasure = {
    stickMeasureId: 1, // 贴地测量 id
    endPoint: [], // 鼠标移动位置
    positions: [], // 点击记录点位信息
    DynamicLine: null, // 动态线实体

    // 贴地测量
    handleStickMeasure() {
        HandleMeasure.positions = [];
        HandleMeasure.DynamicLine = null;
        var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        viewer.clock.shouldAnimate = true;
        HandleMeasure.drawDynamicLine()
        //
        //鼠标左键单击事件
        handler.setInputAction((movement) => {
            // 取消双击事件-追踪该位置
            viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
            // movement.position  屏幕坐标
            // viewer.scene.globe.ellipsoid 笛卡尔坐标
            var cartesian2 = viewer.camera.pickEllipsoid(movement.position, viewer.scene.globe.ellipsoid);
            // cartesian2  射线到球体的交点的 笛卡尔坐标点
            // cartographic  根据笛卡尔坐标点，获取经纬度的弧度值
            var cartographic = Cesium.Cartographic.fromCartesian(cartesian2);
            // 将弧度值转为经纬度
            var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
            var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
            HandleMeasure.positions.push([lng, lat]);

            viewer.entities.add({
                position: cartesian2,
                point: {
                    pixelSize: 8,
                    color: Cesium.Color.DEEPSKYBLUE,
                    outlineColor: Cesium.Color.GOLD,
                    outlineWidth: 2,
                },
            });
            // 创建动态线
            if (HandleMeasure.positions.length >= 2) {
                viewer.entities
                const id = 'DynamicLine' + (HandleMeasure.stickMeasureId - 1);
                const box = viewer.entities.getById(id);
                let n = HandleMeasure.positions.length - 1;

                // HandleMeasure.endPoint[0] = null;
                // HandleMeasure.endPoint[1] = null;




                if (!HandleMeasure.endPoint[0] || !HandleMeasure.endPoint[1]) {
                    return []
                }
                box.position = Cesium.Cartesian3.fromDegreesArray([HandleMeasure.positions[n][0], HandleMeasure.positions[n][1], HandleMeasure.endPoint[0], HandleMeasure.endPoint[1]])
                HandleMeasure.endPoint[0] = null;
                HandleMeasure.endPoint[1] = null;


                // let n = HandleMeasure.positions.length - 1;
                // HandleMeasure.DynamicLine._polyline.setPotions = Cesium.Cartesian3.fromDegreesArray([HandleMeasure.positions[n][0], HandleMeasure.positions[n][1], HandleMeasure.endPoint[0], HandleMeasure.endPoint[1]])

                // HandleMeasure.drawDynamicLine()
            }
            // if (HandleMeasure.positions.length >= 2) {
            //     const n = HandleMeasure.positions.length
            //     var pos = [HandleMeasure.positions[n - 2][0], HandleMeasure.positions[n - 2][1], HandleMeasure.positions[n - 1][0], HandleMeasure.positions[n - 1][1]]
            //     HandleMeasure.drawTheLine(pos)
            // }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        // 鼠标移动事件
        handler.setInputAction((movement) => {
            if (!HandleMeasure.positions.length) return
            // viewer.scene.globe.ellipsoid 笛卡尔坐标
            var cartesian2 = viewer.camera.pickEllipsoid(movement.endPosition, viewer.scene.globe.ellipsoid);
            // cartesian2  射线到球体的交点的 笛卡尔坐标点
            // cartographic  根据笛卡尔坐标点，获取经纬度的弧度值
            var cartographic = Cesium.Cartographic.fromCartesian(cartesian2);
            // 将弧度值转为经纬度
            var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度值
            var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度值
            HandleMeasure.endPoint[0] = lng;
            HandleMeasure.endPoint[1] = lat;
            viewer.scene.postProcessStages.fxaa.enabled = false; //去锯齿
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        // 右键取消测量
        handler.setInputAction((movement) => {
            viewer.entities.remove(HandleMeasure.DynamicLine);
            HandleMeasure.DynamicLine = null;
            handler.destroy()
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
        var doubleHandler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
        doubleHandler.setInputAction(function (movement) {
            var pick = viewer.scene.pick(movement.position);
            if (Cesium.defined(pick) && pick.id._id) {
                var box = viewer.entities.getById(pick.id._id)
                viewer.entities.remove(box)
            }

        }, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    },
    // 将点连城线
    drawTheLine(positions) {
        // const d = HandleMeasure.surfaceDistance(positions)
        viewer.entities.add({
            id: 'measureLine' + HandleMeasure.stickMeasureId++,
            name: "measureLine",
            polyline: {
                show: true,
                positions: Cesium.Cartesian3.fromDegreesArray(positions),
                width: 6,
                // material: Cesium.Color.RED,
                material: new Cesium.PolylineGlowMaterialProperty({
                    color: Cesium.Color.GOLD,
                    glowPower: 0.25,
                }),
            },
        })
        viewer.entities.add({
            position: Cesium.Cartesian3.fromDegrees(positions[2], positions[3]),
            label: {
                text: HandleMeasure.setDistance(HandleMeasure.surfaceDistance(positions)),
                font: '12pt Source Han Sans CN',    //字体样式
                fillColor: Cesium.Color.WHITE,        //字体颜色
                backgroundColor: Cesium.Color.BLACK,    //背景颜色
                showBackground: true,                //是否显示背景颜色
                style: Cesium.LabelStyle.FILL,        //label样式
                outlineWidth: 2,
                verticalOrigin: Cesium.VerticalOrigin.CENTER,//垂直位置
                horizontalOrigin: Cesium.HorizontalOrigin.LEFT,//水平位置
                pixelOffset: new Cesium.Cartesian2(0.0, 20)
            }
        })
    },
    updateEndPoint(time, result) {
        if (!HandleMeasure.endPoint[0] || !HandleMeasure.endPoint[1] || HandleMeasure.positions.length === 0) {
            return []
        } else {
            let n = HandleMeasure.positions.length - 1;
            return Cesium.Cartesian3.fromDegreesArray([HandleMeasure.positions[n][0], HandleMeasure.positions[n][1], HandleMeasure.endPoint[0], HandleMeasure.endPoint[1]], Cesium.Ellipsoid.WGS84, result)
        }
    },
    // 绘制动态线
    drawDynamicLine() {
        viewer.entities.add({
            id: 'DynamicLine' + HandleMeasure.stickMeasureId++,
            name: "DynamicLine",
            polyline: {
                positions: new Cesium.CallbackProperty(HandleMeasure.updateEndPoint, false),
                width: 6,
                material: new Cesium.PolylineGlowMaterialProperty({
                    color: Cesium.Color.GOLD,
                    glowPower: 0.25,
                }),
            },
        })
    },
    // 距离单位配置
    setDistance(d) {
        if (d <= 1000) {
            return d + '米'
        } else if (d > 1000) {
            return (d / 1000).toFixed(2) + '公里'
        }
    },
    // 计算两点距离
    getSpaceDistance(positions) {
        var distance = 0;
        for (var i = 0; i < positions.length - 1; i++) {
            /**根据经纬度计算出距离**/
            var geodesic = new Cesium.EllipsoidGeodesic();
            geodesic.setEndPoints(positions[i], positions[i + 1]);
            var s = geodesic.surfaceDistance;
            //返回两点之间的距离
            s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
            distance = distance + s;
        }

        return distance
    },
    // 求两点的地表距离
    surfaceDistance(positions) {
        let startPosition = new Cesium.Cartographic(positions[0] / (180 / Math.PI), positions[1] / (180 / Math.PI));
        let endPosition = new Cesium.Cartographic(positions[2] / (180 / Math.PI), positions[3] / (180 / Math.PI));
        let geodesic = new Cesium.EllipsoidGeodesic(startPosition, endPosition);
        let distance = geodesic.surfaceDistance; // 单位是米
        return distance.toFixed(2)
    },
    // 求两点的直线距离
    linearDistance() {
        let startPosition = new Cesium.Cartesian3.fromDegrees(110, 30);
        let endPosition = new Cesium.Cartesian3.fromDegrees(110, 30.1);
        let distance = Cesium.Cartesian3.distance(startPosition, endPosition);
    }
}
export default HandleMeasure